package com.dkd.flow.utils;

import com.dkd.flow.domain.EProcessTask;
import com.dkd.flow.emums.FlowOperation;
import com.dkd.flow.emums.TaskStatus;
import com.dkd.flow.emums.TaskType;
import com.dkd.flow.emums.TaskActionType;
import com.dkd.flow.mapper.EProcessTaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

@Service
@Component
public class FlowOperationUtils {
    @Autowired
    EProcessTaskMapper eProcessTaskMapper;
    @Autowired
    RevokeUtils revokeUtils;
    // 哪些状态代表已经处理完，不允许继续操作
    private static final Set<TaskStatus> PROCESSED_STATUS = EnumSet.of(TaskStatus.REJECT_AND_END,
            TaskStatus.PREVIEW);

    /**
     * 判断某个操作在当前任务状态下是否合法
     *
     * @param operation  操作类型
     * @param taskStatus 当前任务状态
     * @return true表示非法操作（不能提交）；false表示合法（可以提交）
     */
    public static boolean isIllegalOperation(FlowOperation operation,
            TaskStatus taskStatus) {
        return false;
    }

    public ArrayList<Integer> getFlowOperationBtnShow(Long taskStatus,
            Long taskType,
            Long curTaskId) {
        // 允许的操作
        ArrayList<Integer> flowOperationBtnShow = new ArrayList<>();
        boolean isAllowRevoke = false;
        if (curTaskId != null) {
            // 发起人任务
            if (taskType == TaskType.INITIATOR_TASK.getValue().longValue()) {
                // 草稿 DRAFT 11 已撤回 RECALL 10
                if (taskStatus == TaskStatus.DRAFT.getValue().longValue()
                        || taskStatus == TaskStatus.RECALL.getValue().longValue()) {
                    flowOperationBtnShow.add(FlowOperation.LAUNCH.getValue());
                    flowOperationBtnShow.add(FlowOperation.SAVE.getValue());
                    flowOperationBtnShow.add(FlowOperation.CC.getValue());
                }
                // 因为都是审批中，所以要取操作任务的状态，是驳回，还是驳回所有
                else if (taskStatus == TaskStatus.UNDER_APPROVAL.getValue().longValue()) {
                    EProcessTask curTask = eProcessTaskMapper.selectById(curTaskId);
                    if (curTask != null) {
                        // 查找操作任务（taskActionType为1的任务）
                        QueryWrapper<EProcessTask> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("TASK_GROUP_ID", curTask.getTaskGroupId());
                        queryWrapper.eq("TASK_ACTION_TYPE", TaskActionType.OPERATION_TASK.getValue());
                        queryWrapper.eq("TASK_STATUS", TaskStatus.RETURNED.getValue());
                        List<EProcessTask> operationTasks = eProcessTaskMapper.selectList(queryWrapper);

                        if (!operationTasks.isEmpty()) {
                            // 如果找到驳回操作的任务，显示重新提交
                            flowOperationBtnShow.add(FlowOperation.RE_SUBMIT.getValue());
                        } else {
                            // 查找驳回所有的操作任务
                            queryWrapper = new QueryWrapper<>();
                            queryWrapper.eq("TASK_GROUP_ID", curTask.getTaskGroupId());
                            queryWrapper.eq("TASK_ACTION_TYPE", TaskActionType.OPERATION_TASK.getValue());
                            queryWrapper.eq("TASK_STATUS", TaskStatus.RETURNED_ALL.getValue());
                            operationTasks = eProcessTaskMapper.selectList(queryWrapper);

                            if (!operationTasks.isEmpty()) {
                                // 如果找到驳回所有的操作任务，显示重新提交所有
                                flowOperationBtnShow.add(FlowOperation.RE_SUBMIT_ALL.getValue());
                            }
                        }
                        flowOperationBtnShow.add(FlowOperation.SAVE.getValue());
                        flowOperationBtnShow.add(FlowOperation.CC.getValue());
                    }
                }
                // 发起 INITIATE_APPLICATION 1
                else if (taskStatus == TaskStatus.INITIATE_APPLICATION.getValue().longValue()) {
                    // 当前任务是否允许操作撤回
                    isAllowRevoke = revokeUtils.checkFollowingTasksNotProcessed(curTaskId);
                    if (isAllowRevoke) {
                        flowOperationBtnShow.add(FlowOperation.REVOKE.getValue());
                    }
                }
            }
            // 审批人任务，转交人任务
            else if (taskType == TaskType.APPROVER_TASK.getValue().longValue()
                    || taskType == TaskType.TRANSFER_TASK.getValue().longValue()) {
                // 审批中 UNDER_APPROVAL 2
                if (taskStatus == TaskStatus.UNDER_APPROVAL.getValue().longValue()) {
                    flowOperationBtnShow.add(FlowOperation.SAVE.getValue());
                    flowOperationBtnShow.add(FlowOperation.CC.getValue());
                    flowOperationBtnShow.add(FlowOperation.AGREE.getValue());
                    flowOperationBtnShow.add(FlowOperation.REJECT_AND_END.getValue());
                    flowOperationBtnShow.add(FlowOperation.TRANSFER.getValue());
                    flowOperationBtnShow.add(FlowOperation.RETURNED.getValue());
                    flowOperationBtnShow.add(FlowOperation.RETURNED_ALL.getValue());
                }
                // 已审批 APPROVAL 3
                else if (taskStatus == TaskStatus.APPROVAL.getValue().longValue()) {
                    // 当前任务是否允许操作撤回
                    isAllowRevoke = revokeUtils.checkFollowingTasksNotProcessed(curTaskId);
                    if (isAllowRevoke) {
                        flowOperationBtnShow.add(FlowOperation.REVOKE.getValue());
                    }
                }
                // 已驳回 RETURNED 6
                else if (taskStatus == TaskStatus.RETURNED.getValue().longValue()) {
                    // 当前任务是否允许操作撤回
                    isAllowRevoke = revokeUtils.checkFollowingTasksNotProcessed(curTaskId);
                    if (isAllowRevoke) {
                        flowOperationBtnShow.add(FlowOperation.REVOKE.getValue());
                    }
                }
                // 已驳回所有 RETURNED_ALL 7
                else if (taskStatus == TaskStatus.RETURNED_ALL.getValue().longValue()) {
                    // 当前任务是否允许操作撤回
                    isAllowRevoke = revokeUtils.checkFollowingTasksNotProcessed(curTaskId);
                    if (isAllowRevoke) {
                        flowOperationBtnShow.add(FlowOperation.REVOKE.getValue());
                    }
                }
                // 已转交 TRANSFERRED 8
                else if (taskStatus == TaskStatus.TRANSFERRED.getValue().longValue()) {
                    // 当前任务是否允许操作撤回
                    isAllowRevoke = revokeUtils.checkFollowingTasksNotProcessed(curTaskId);
                    if (isAllowRevoke) {
                        flowOperationBtnShow.add(FlowOperation.REVOKE.getValue());
                    }
                }
                // 已拒绝并关闭 REJECT_AND_END 4
                else if (taskStatus == TaskStatus.REJECT_AND_END.getValue().longValue()) {
                    // 当前任务是否允许操作撤回
                    isAllowRevoke = revokeUtils.checkFollowingTasksNotProcessed(curTaskId);
                    if (isAllowRevoke) {
                        flowOperationBtnShow.add(FlowOperation.REVOKE.getValue());
                    }
                }
            }
        }
        // 发起人发起流程
        else {
            flowOperationBtnShow.add(FlowOperation.LAUNCH.getValue());
            flowOperationBtnShow.add(FlowOperation.SAVE.getValue());
            flowOperationBtnShow.add(FlowOperation.CC.getValue());
        }
        return flowOperationBtnShow;
    }
}
